package com.naughtybabystudio.android.shaketheworld;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

public class NBS_ShakeTheWorldActivity extends Activity {

	private static final String TAG = "ShakeTheWorld";
    private static final boolean D = true;
    
    private float mScreenBrightness;
    private float mKeyBrightness;
    
    private DisplaySurfaceView mDisplayView;
    private DisplayContent mDisplay;

	@Override
	protected void onStart() {
		if(D) Log.e(TAG, "++ ON START ++");
		super.onStart();
	}

	@Override
	protected void onResume() {
		if(D) Log.e(TAG, "++ ON RESUME ++");
		super.onResume();
		
		mScreenBrightness=getScreenBrightness();
		setScreenBrightnessMax();
		
		mKeyBrightness=getKeyBrightness();
		setKeyBrightnessOff();
		
		if(mDisplayView!=null)
			mDisplayView.resume();
	}

    @Override
	protected void onPause() {
    	if(D) Log.e(TAG, "++ ON PAUSE ++");
		super.onPause();
		
		setScreenBrightness(mScreenBrightness);
		setKeyBrightness(mKeyBrightness);
		if(mDisplayView!=null)
			mDisplayView.pause();
	}
    
	@Override
	protected void onStop() {
		if(D) Log.e(TAG, "++ ON STOP ++");
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		if(D) Log.e(TAG, "++ ON DESTORY ++");
		super.onDestroy();
	}
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setFullScreen();
        
        DisplayMetrics dm=getResources().getDisplayMetrics();
        mDisplay=new DisplayContent(dm.widthPixels,dm.heightPixels);
        mDisplayView=new DisplaySurfaceView(this,mDisplay);
        
        
        setContentView(mDisplayView);
//        setContentView(R.layout.main);
        
    }
    
    //functions for full screen
    private void setFullScreen() {
    	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
    			WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
        		WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }
    
    //functions for screen brightness
    private float getScreenBrightness() {   
    	WindowManager.LayoutParams lp = getWindow().getAttributes();
    	return lp.screenBrightness;   
    }
    private void setScreenBrightness(float brightness) {   
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = brightness;
        getWindow().setAttributes(lp);
   }
    private void setScreenBrightnessMax() {
    	setScreenBrightness(1.0f);
    }
    
    //functions for key brightness
    private float getKeyBrightness() {
    	WindowManager.LayoutParams lp = getWindow().getAttributes();
    	return lp.buttonBrightness;   
    }
    private void setKeyBrightness(float brightness) {   
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.buttonBrightness = brightness;
        getWindow().setAttributes(lp);
   }
    private void setKeyBrightnessOff() {
    	setKeyBrightness(0);
    }
    
    //functions for option menu creation
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}


}
